package com.google.coffeeshop.business;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;

import vn.bang.utility.JpaUtil;

import com.google.coffeeshop.GeneralConstants;
import com.google.coffeeshop.dao.DAOFactory;
import com.google.coffeeshop.entity.OrderDTO;
import com.google.coffeeshop.entity.SeatDTO;
import com.google.coffeeshop.log.Log;

public class MaintenanceBusiness extends AbstractBaseBusiness {
	
	static public SeatDTO findSeatById(long seatId) {
		SeatDTO seat = null;
		final EntityManager entityManager = createEntityManager();
		try {
			seat = getSeatById(seatId, entityManager);
		} catch(PersistenceException persistenceException) {
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
		return seat;
	}
	
	static public boolean startSeat(final long seatId) {
		final EntityManager entityManager = createEntityManager();
		try {
			JpaUtil.beginTransaction(entityManager);
			final SeatDTO seat = getSeatById(seatId, entityManager);
			if (isSeatNew(seat)) {
				changeSeatStatus(seat, GeneralConstants.SEAT_USABLE_STATUS, entityManager);
			}
			JpaUtil.commitEntityManager(entityManager);
			return Boolean.TRUE;
		} catch(PersistenceException persistenceException) {
			JpaUtil.rollbackEntityManager(entityManager);
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
		return Boolean.FALSE;
	}
	
	static public boolean destroySeat(final long seatId) {
		final EntityManager entityManager = createEntityManager();
		try {
			JpaUtil.beginTransaction(entityManager);
			final SeatDTO seat = getSeatById(seatId, entityManager);
			if (isSeatStopped(seat)) {
				changeSeatStatus(seat, GeneralConstants.SEAT_TERMINATED_STATUS, entityManager);
				JpaUtil.commitEntityManager(entityManager);
				return Boolean.TRUE;
			}
		} catch(PersistenceException persistenceException) {
			JpaUtil.rollbackEntityManager(entityManager);
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
		return Boolean.FALSE;
	}
	
	static public boolean reuseSeat(final long seatId) {
		final EntityManager entityManager = createEntityManager();
		try {
			JpaUtil.beginTransaction(entityManager);
			final SeatDTO seat = getSeatById(seatId, entityManager);
			if (isSeatStopped(seat)) {
				changeSeatStatus(seat, GeneralConstants.SEAT_USABLE_STATUS, entityManager);
				JpaUtil.commitEntityManager(entityManager);
				return Boolean.TRUE;
			}
			
		} catch(PersistenceException persistenceException) {
			JpaUtil.rollbackEntityManager(entityManager);
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
		return Boolean.FALSE;
	}

	static public boolean maintainSeat(final long seatId) {
		final EntityManager entityManager = createEntityManager();
		try {
			JpaUtil.beginTransaction(entityManager);
			final SeatDTO seat = DAOFactory.createSeatDAO(entityManager).find(seatId);
			if (isSeatUsable(seat)) {
				seat.setStatus(GeneralConstants.SEAT_STOP_STATUS);
				DAOFactory.createSeatDAO(entityManager).merge(seat);
				JpaUtil.commitEntityManager(entityManager);
				return Boolean.TRUE;
			}
		} catch(PersistenceException persistenceException) {
			JpaUtil.rollbackEntityManager(entityManager);
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
		return Boolean.FALSE;
	}
	
	static public void transferSeatToSeat(final long firstSeatId, final long secondSeatId) {
		final EntityManager entityManager = createEntityManager();
		try {
			JpaUtil.beginTransaction(entityManager);
			
			SeatDTO firstSeat = DAOFactory.createSeatDAO(entityManager).find(firstSeatId);
			if (firstSeat == null) {
				return;
			}
			SeatDTO secondSeat = DAOFactory.createSeatDAO(entityManager).find(secondSeatId);
			if (secondSeat == null) {
				return;
			}
			OrderDTO firstOrder = DAOFactory.createOrderDAO(entityManager).findBySeatId(firstSeatId, GeneralConstants.ORDER_UNPROCESSED_STATUS);
			OrderDTO secondOrder = DAOFactory.createOrderDAO(entityManager).findBySeatId(secondSeatId, GeneralConstants.ORDER_UNPROCESSED_STATUS);
			
			
			if (firstOrder != null) {
				firstOrder.setSeat(secondSeat);
			}
			if (secondOrder != null) {
				secondOrder.setSeat(firstSeat);
			}
			swapStatus(firstSeat, secondSeat);
			
			DAOFactory.createOrderDAO(entityManager).save(firstOrder);
			DAOFactory.createOrderDAO(entityManager).save(secondOrder);
			DAOFactory.createSeatDAO(entityManager).save(firstSeat);
			DAOFactory.createSeatDAO(entityManager).save(secondSeat);
			
			JpaUtil.commitEntityManager(entityManager);
			
		} catch(PersistenceException persistenceException) {
			JpaUtil.rollbackEntityManager(entityManager);
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
	}
	
	private static void swapStatus(SeatDTO firstSeat, SeatDTO secondSeat) {
		Integer temp = firstSeat.getStatus();
		firstSeat.setStatus(secondSeat.getStatus());
		secondSeat.setStatus(temp);
	}
	
}
